/*
* 赵晓莉
* */
require(["jquery","template","bootstrap3.min","bbcare","scprit/followup/app/comm/page/page.js"],function($,template,bootstrap,$bb,$page) {
    var URL = $bb.baseUrl("department")+"followup/service";
    var count=20;
    //加载统计数据：
    $bb.ajax({
        url: URL + "/patient/familyreport/patientDataStatistics",
        type: "post",
        contentType: "application/json;charset=UTF-8",
        dataType: "json",
        data: JSON.stringify({
            data: {
                "type":"3"
            }
        }),
        success: function (data) {
            var watchList=$(".monitor-report-list>div");
            watchList.eq(1).find(".monitor-report-val").html(data.data.dayTestPatients);
            watchList.eq(2).find(".monitor-report-val").html(data.data.totalTestPatients);
            watchList.eq(3).find(".monitor-report-val").html(data.data.abnormalTimes);
            watchList.eq(4).find(".monitor-report-val").html(data.data.totalTestTimes);
            $(".monitor-report-footing").find(".totalNum").html(data.data.totalTestTimes);
        },
        error: function (data) {}
    });
    $("#toAddInfo").click(function(){
        $bb.openPage("addGrowingRecord");
    });
    //加载页面：
    function growth(val,dom){
        var lens=val.length;
        var lis='';
        var emptyLis="";
        for(var i=0;i<lens;i++){
            if(!val[i].headCircumference){
                val[i].headCircumference='暂无';
            }
            if(!val[i].height){
                val[i].height='暂无';
            }
            if(!val[i].weight){
                val[i].weight='暂无';
            }
            var img;
            if(val[i].gender=="N"){
                img='<img src="page/department/img/boygrey.png"/>';
            }else{
                img='<img src="page/department/img/girlgrey.png"/>';
            }
            var username=nameDeal(val[i].userName);
            lis+='<div class="monitor-user-card" growthRecordId="'+val[i].growthRecordId+'" userId="'+val[i].userId+'">\
                        <div class="card-heading">'+img+'\
                            <font class="user-name">'+username+'</font>\
                            <font class="pull-right monitor-time">'+val[i].testTimeDesc+'</font>\
                         </div>\
                    <div class="card-body">\
                        <div>\
                            <div class="monitor-number">'+val[i].height+'</div>\
                            <div class="monitor-label">身高（cm）</div>\
                        </div>\
                        <div>\
                            <div class="monitor-number">'+val[i].weight+'</div>\
                            <div class="monitor-label">体重（kg)</div>\
                         </div>\
                        <div>\
                            <div class="monitor-number">'+val[i].headCircumference+'</div>\
                            <div class="monitor-label">头围（cm）</div>\
                        </div>\
                    </div>\
            </div>'
        }
        for(var i=0;i<count-lens;i++){
            emptyLis+='<div class="monitor-user-card"></div>';
        }
        var li=lis+emptyLis;
        $(dom).empty().append(li);
    }

    function getGrowth(page,rows){
        $bb.ajax({
            url: URL + "/patient/growth/patientListqry",
            type: "post",
            contentType: "application/json;charset=UTF-8",
            dataType: "json",
            data: JSON.stringify({
                data: {
                    "page":page,
                    "rows":rows
                }
            }),
            success: function (data) {
                var pages=Math.ceil(data.total/rows);
                var value=data.rows;
                growth(value,".monitor-user-box");
                $page.init({
                    id:"btn-toolbar",
                    pagesAll:pages,
                    page:page
                });
                $("#btn-toolbar .btn").click(function(){
                    var text = $(this).text();
                    page = $page.getPageActive({
                        "pageClick":text,
                        "page":page,
                        "pagesAll":pages
                    });
                    getGrowth(page,rows);
                });
            },
            error: function (data) {}
        });
    }
    getGrowth("1",count);
    //页面跳转：
    $("#monitor-user-box1").on("click",".monitor-user-card",function(){
        var growthRecordId=$(this).attr("growthRecordId");
        var userId=$(this).attr("userId");
        var obj={
            "userId":userId,
            "growthRecordId":growthRecordId
        }
        if(obj.userId && obj.growthRecordId){
            $bb.openPage("growthDevelopmentDetails",obj);
        }
    });
    $("#getAll").click(function(){
        $("#key").val("");
        getGrowth("1",count);
    });

    $(".input-group-addon").click(function(){
        var key=$("#key").val();
        toSearch(key,count);
    });
    $("#key").on("input propertychange",function(){
        var key=$.trim($(this).val());
        toSearch(key,count);
    });
    function toSearch(key,rows,page){
        $bb.ajax({
            url: URL + "/patient/growth/patientListqry",
            type: "post",
            contentType: "application/json;charset=UTF-8",
            dataType: "json",
            data: JSON.stringify({
                data: {
                    "page":page,
                    "rows":rows,
                    "userName":key
                }
            }),
            success: function (data) {
                var pages=Math.ceil(data.total/rows);
                var value=data.rows;
                growth(value,".monitor-user-box");

                $page.init({
                    id:"btn-toolbar",
                    pagesAll:pages,
                    page:page
                });
                $("#btn-toolbar .btn").click(function(){
                    var text = $(this).text();
                    page = $page.getPageActive({
                        "pageClick":text,
                        "page":page,
                        "pagesAll":pages
                    });
                    toSearch(key,rows,page);
                });
            },
            error: function (data){}
        });
    }
    //姓名截取前三个字：
    function nameDeal(name){
        var username;
        var len=name.length;
        if(len>3){
            username=name.substring(0,3);
        }else{
            username=name
        }
        return username;
    }
});
